/**
 * 部门管理相关API接口
 */
import { request } from '@/utils/request'
import type { ApiResponse } from '@/types/api'

export interface Department {
  id: string
  name: string
  parent_id?: string
  manager_id?: string
  manager_name?: string
  order: number
  description?: string
  children?: Department[]
  user_count?: number
  created_at?: string
  updated_at?: string
}

export interface DepartmentCreate {
  name: string
  parent_id?: string
  manager_id?: string
  order?: number
  description?: string
}

export interface DepartmentUpdate {
  name?: string
  parent_id?: string
  manager_id?: string
  order?: number
  description?: string
}

export interface DepartmentMoveParams {
  new_parent_id?: string
  new_order?: number
}

// 获取部门列表
export function getDepartmentList(params?: { tree?: boolean; include_users?: boolean }): Promise<ApiResponse<Department[]>> {
  return request({
    url: '/departments',
    method: 'get',
    params
  })
}

// 获取部门树
export function getDepartmentTree(params?: { include_users?: boolean }): Promise<ApiResponse<Department[]>> {
  return request({
    url: '/departments/tree',
    method: 'get',
    params
  })
}

// 获取部门详情
export function getDepartment(id: string): Promise<ApiResponse<Department>> {
  return request({
    url: `/departments/${id}`,
    method: 'get'
  })
}

// 创建部门
export function createDepartment(data: DepartmentCreate): Promise<ApiResponse<Department>> {
  return request({
    url: '/departments',
    method: 'post',
    data
  })
}

// 更新部门
export function updateDepartment(id: string, data: DepartmentUpdate): Promise<ApiResponse<Department>> {
  return request({
    url: `/departments/${id}`,
    method: 'put',
    data
  })
}

// 删除部门
export function deleteDepartment(id: string): Promise<ApiResponse<void>> {
  return request({
    url: `/departments/${id}`,
    method: 'delete'
  })
}

// 移动部门
export function moveDepartment(id: string, params: DepartmentMoveParams): Promise<ApiResponse<Department>> {
  return request({
    url: `/departments/${id}/move`,
    method: 'post',
    data: params
  })
}

